package com.travel.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.util.ResultMsg;
import com.travel.entity.TbHome;
import com.travel.entity.TbHomeOrder;
import com.travel.entity.TbUser;
import com.travel.exception.OrderAddFailureException;
import com.travel.mapper.HomeMapper;
import com.travel.mapper.HomeOrderMapper;
import com.travel.mapper.UserMapper;
import com.travel.service.HomeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
public class HomeOrderServiceImpl extends ServiceImpl<HomeOrderMapper,TbHomeOrder> implements HomeOrderService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    HomeMapper homeMapper;

    @Transactional(rollbackFor = OrderAddFailureException.class)
    @Override
    public ResultMsg addOrderClass(TbUser user, TbHome home) {

        TbHomeOrder homeOrder = new TbHomeOrder();

        Snowflake snowflake = IdUtil.getSnowflake(114514, 1919810);
        String s = snowflake.nextIdStr();
        homeOrder.setHomeOrderId(s);
        homeOrder.setUserId(user.getUserId());
        homeOrder.setHomeOrderTime(new Date());
        homeOrder.setHomeOrderName(home.getHomeName());
        homeOrder.setHomeOrderType(home.getHomeType());
        homeOrder.setHomeOrderPrice(home.getHomePrice());
        homeOrder.setHomeOrderStatus(0);
        homeOrder.setHomeId(home.getHomeId());
        boolean saveOrder = super.save(homeOrder);

        home.setHomeStatus(1);
        int updateHome = homeMapper.updateById(home);

        if (saveOrder==true && updateHome==1){
            return ResultMsg.success(200);
        }
        throw new OrderAddFailureException();
    }

    @Transactional(rollbackFor = OrderAddFailureException.class)
    @Override
    public ResultMsg addOrder(Integer userId, Integer homeId) {
        if (userId==null){
            throw new OrderAddFailureException();
        }
        QueryWrapper<TbHome> homeWrapper = new QueryWrapper<>();
        homeWrapper.eq("home_id",homeId);
        TbHome home = homeMapper.selectOne(homeWrapper);

        TbHomeOrder homeOrder = new TbHomeOrder();
        Snowflake snowflake = IdUtil.getSnowflake(11, 19);
        String s = snowflake.nextIdStr();
        homeOrder.setHomeOrderId(s);
        homeOrder.setUserId(userId);
        homeOrder.setHomeOrderTime(new Date());
        homeOrder.setHomeOrderName(home.getHomeName());
        homeOrder.setHomeOrderType(home.getHomeType());
        homeOrder.setHomeOrderPrice(home.getHomePrice());
        homeOrder.setHomeOrderStatus(0);
        homeOrder.setHomeId(home.getHomeId());
        boolean saveOrder = super.save(homeOrder);

        home.setHomeStatus(0);
        int updateHome = homeMapper.updateById(home);

        if (saveOrder==true && updateHome==1){
            return ResultMsg.success(200);
        }
        throw new OrderAddFailureException();
    }


    @Override
    public ResultMsg getAll() {
        List<TbHomeOrder> orderList = super.list();
        if (orderList!=null || orderList.size()!=0){
            return ResultMsg.success(orderList);
        }
        return ResultMsg.error(306);
    }

    @Override
    public ResultMsg getOneById(Integer homeOrderId) {
        TbHomeOrder homeOrder = super.getById(homeOrderId);
        if (homeOrder!=null) {
            return ResultMsg.success(homeOrder);
        }
        return ResultMsg.error(306);
    }

    @Override
    public ResultMsg getAllByPage(Integer current, Integer size) {
        Page<TbHomeOrder> orderPage = new Page<>();
        Page<TbHomeOrder> page = super.page(orderPage);
        List<TbHomeOrder> records = page.getRecords();

        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        List<TbUser> userList = userMapper.selectList(wrapper);
        HashMap<Integer, String> map = new HashMap<>();

        for (TbUser user : userList) {
            map.put(user.getUserId(),user.getUserName());
        }

        for (TbHomeOrder record : records) {
            record.setUserName(map.get(record.getUserId()));
        }

        if (page!=null || page.getSize()!=0){
            return ResultMsg.success(page);
        }
        return ResultMsg.error(306);
    }

    @Override
    public ResultMsg insertOne(TbHomeOrder homeOrder) {
        boolean save = super.save(homeOrder);
        if (save){
            return ResultMsg.success();
        }
        return ResultMsg.error(306,"插入失败");

    }

    @Override
    public ResultMsg updateOne(TbHomeOrder homeOrder) {
        boolean update = super.updateById(homeOrder);
        if (update){
            return ResultMsg.success();
        }
        return ResultMsg.error(306,"修改失败");
    }

    @Override
    public ResultMsg deleteOne(String homeOrderId) {
        boolean remove = super.removeById(homeOrderId);
        if (remove){
            return ResultMsg.success();
        }
        return ResultMsg.error(306,"删除失败");
    }

    @Override
    public ResultMsg getByUid(Integer userId) {
        QueryWrapper<TbHomeOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("home_order_status",0);
        List<TbHomeOrder> orderList = super.list(wrapper);
        if (orderList!=null || orderList.size()!=0){
            return ResultMsg.success(orderList);
        }
        return ResultMsg.error(306);
    }

    @Override
    public ResultMsg homeOrderPay(String homeOrderId) {
        TbHomeOrder homeOrder = super.getById(homeOrderId);
        homeOrder.setHomeOrderStatus(1);
        boolean update = super.updateById(homeOrder);
        if (update){
            return ResultMsg.success();
        }
        return ResultMsg.error(306,"修改失败");
    }

    @Override
    public ResultMsg getByOid(String homeOrderId) {
        TbHomeOrder order = super.getById(homeOrderId);
        if (order!=null) {
            return ResultMsg.success(order);
        }
        return ResultMsg.error(306);
    }



}
